Back to the homepage

Column

Lineplot

Column

Barplot

Boxplot

---
title: "Flexdashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source: embed
---
Back to the [homepage](index.html)
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(lubridate)
```

```{r}
# load in dataset
data(instacart)
```
```{r}
# I am interested in exploring reorders in each department and their distributions through out a day/week.
instacart = 
  instacart %>% 
  select(reordered, order_dow, order_hour_of_day, product_name, department)  # only select necessary columns
```


Column {data-width=650}
-----------------------------------------------------------------------

### Lineplot

```{r, warning=FALSE}
# distribution of number of reorders during a day across a week
instacart %>% 
  filter(reordered == 1) %>% 
  mutate(order_dow = wday(order_dow+1, label = TRUE, abbr = FALSE)) %>% 
  group_by(order_dow, order_hour_of_day) %>%
  summarize(n_obs = n()) %>% 
  plot_ly(x = ~order_hour_of_day, y = ~n_obs, color = ~order_dow, 
          type = "scatter", colors = "viridis", mode = "markers") %>% 
  add_lines(showlegend=FALSE) %>% 
  layout(
    title = "Number of Reorders: Each Day Across Week",
    xaxis = list(title="Hour in a day"),
    yaxis = list(title="Number of Reorders")
  )
```

Column {data-width=350}
-----------------------------------------------------------------------

### Barplot

```{r, message=FALSE}
# department and numbers of reorders in total
instacart %>% 
  count(department, reordered) %>% 
  filter(reordered == 1) %>%  # only focus on reorders
  mutate(department = fct_reorder(department, n)) %>% 
  plot_ly(x = ~department, y = ~n, color = ~department, type = "bar", colors = "viridis") %>% 
  layout(
    title = "Numbers of Reorders in Each Department",
    xaxis = list(title="Department Name"),
    yaxis = list(title="# of Reorders")
  )
```

### Boxplot

```{r}
# hour of the day that reorders happens
instacart %>% 
  filter(reordered == 1) %>% 
  group_by(order_dow) %>% 
  mutate(order_dow = wday(order_dow+1, label = TRUE, abbr = FALSE)) %>%  # re-code the variable order_dow
  plot_ly(x = ~order_dow, y = ~order_hour_of_day, color = ~order_dow, type = "box", colors = "viridis") %>% 
  layout(
    title = "Reordered Hour: Each Day of the Week",
    xaxis = list(title="Day of the Week"),
    yaxis = list(title="Hour in a Day")
  )
```